<template>
    <view>
        <!--搜索框-->
        <view class="search-box">
            <uni-search-bar @input="input" :radius="100" cancelButton="none" clearButton="auto"></uni-search-bar>
        </view>
        <!--搜索建议列表-->
        <view class="suggest-list" v-if="searchResults.length !== 0">
            <view class="suggest-item" v-for="(item ,i) in searchResults" :key="i"
                  @click="gotoGoodsDetailHandler(item.goods_id)">
                <text class="suggest-item-text">{{ item.goods_name }}</text>
                <uni-icons class="suggest-item-icon" type="right"></uni-icons>
            </view>
        </view>
        <!--搜索历史列表-->
        <view class="history-list" v-else>
            <view class="action-bar">
                <view class="">搜索历史</view>
                <uni-icons class="" type="trash" size="20" @click="clearHistory"></uni-icons>
            </view>
            <view class="history-item">
                <text v-for="(item,i) in histories" :key="i" @click="gotoGoodsList(item)"> {{ item }}</text>
            </view>
        </view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                timer: null,
                keyword: '',
                searchResults: [],
                historyList: []
            }
        },
        methods: {
            // input 事件的处理函数
            input(e) {
                clearTimeout(this.timer)
                this.timer = setTimeout(() => {
                    this.keyword = e
                    this.getSearchList()
                }, 500)
            },
            async getSearchList() {
                if (this.keyword.length === 0) {
                    this.searchResults = []
                    return false
                }
                const res = await uni.$http.get('/api/public/v1/goods/qsearch', {query: this.keyword})
                if (res.data.meta.status !== 200) return uni.$showMsg()
                this.searchResults = res.data.message
                this.saveHistoryList()
            },
            gotoGoodsDetailHandler(goods_id) {
                uni.navigateTo({
                    url: `/subpackage/goods_detail/goods_detail?goods_id=${goods_id}`
                })
            },
            gotoGoodsList(keyword){
                uni.navigateTo({
                    url: `/subpackage/goods_list/goods_list?query=${keyword}`
                })
            },
            saveHistoryList() {
                // this.historyList.push(this.keyword)
                // 将historyList数组转换为Set对象
                const set = new Set(this.historyList)
                // 调用Set对象的delete方法移除元素
                set.delete(this.keyword)
                // 调用Set对象的add方法添加元素
                set.add(this.keyword)
                // 将Set对象转换为historyList数组
                this.historyList = Array.from(set)
                // 调用setStorageSync()方法持久化到本地
                uni.setStorageSync('keyword',JSON.stringify(this.historyList))
            },
             clearHistory() {
                uni.showModal({
                    title:'温馨提示！',
                    content:'您确定要删除最近搜索记录吗？',
                    confirmColor:'#ff93a7',
                    success: () => {
                        this.historyList = []
                        uni.setStorageSync('keyword', '[]')
                    }
                    /*success: function () {
                        this.historyList = []
                         uni.setStorageSync('keyword', '[]')
                    }.bind(this)*/
                })
            }
        },
        computed: {
            histories() {
                return [...this.historyList].reverse()
            }
        },
        onLoad() {
            this.historyList = JSON.parse(uni.getStorageSync('keyword') || '[]')
        },
    }
</script>

<style lang="scss">
    .search-box {
        position: sticky;
        top: 0;
        z-index: 999;
    }

    .suggest-list {
        padding: 0 5px;

        .suggest-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 10px 0;
            border-bottom: 1px solid #efefef;

            .suggest-item-text {
                font-size: 12px;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
            }
        }
    }

    .history-list {
        .action-bar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 10px 5px;
            border-bottom: 1px solid #f2f2f2;
            font-size: 14px;
        }

        .history-item {
            display: flex;
            flex-wrap: wrap;

            text {
                width: fit-content;
                background-color: #f8f8f8;
                padding: 8px 10px;
                margin: 8px 0 0 8px;
                border-radius: 15px;
                font-size: 14px;
            }
        }
    }
</style>
